package com.polaris.live.utils

import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.manager.AppConfigManager

/**
 * AppConfigUtils
 *
 * @author Created by lucas on 2024/4/7 13:04
 */
object AppConfigUtils {

    private var mShowPhoneCountries: Set<String>? = null
    private var mHidePhoneCountries: Set<String>? = null
    private var mDecorationNames: MutableMap<String, String>? = null
    private fun getShowPhoneCountries(): Set<String>? {
        return mShowPhoneCountries ?: AppConfigManager.getConfig(CommonConst.Configs.showPhoneCountries)
            ?.let { JsonUtils.fromJson<Set<String>>(it) }
            .also { mShowPhoneCountries = it ?: emptySet() }
    }

    private fun getHidePhoneCountries(): Set<String>? {
        return mHidePhoneCountries ?: AppConfigManager.getConfig(CommonConst.Configs.hidePhoneCountries)
            ?.let { JsonUtils.fromJson<Set<String>>(it) }
            .also { mHidePhoneCountries = it ?: emptySet() }
    }

    /**
     * 是否显示手机号
     */
    fun isShowPhoneButton(): Boolean {
        if (CacheUtil.isPhoneNumberShowed() == true) {
            return true
        }

        val showPhoneCountries = getShowPhoneCountries()
        return if (!showPhoneCountries.isNullOrEmpty()) {
            DmaUtils.currentContainsCountry(showPhoneCountries)
        } else {
            val hidePhoneCountries = getHidePhoneCountries()
            if (!hidePhoneCountries.isNullOrEmpty()) {
                !DmaUtils.currentContainsCountry(hidePhoneCountries)
            } else {
                true
            }
        }
    }

    /**
     * 背包对应的类目名称
     */
    fun getDecorationNames(): MutableMap<String, String>? {
        return mDecorationNames ?: AppConfigManager.getConfig(CommonConst.Configs.decorationNames)
            ?.let { JsonUtils.fromJson<MutableMap<String, String>>(it) }
            .also { mDecorationNames = it ?: mutableMapOf() }
    }
}